package LUCKY.Android.DataAccess;

import LUCKY.Android.BusinessEntities.EmpresaBE;
import LUCKY.Android.BusinessEntities.TipoBE;
import LUCKY.Android.Util.FunctionsUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

public class TIPOTABLE extends DataTableGIM {
	
	public static final String KEY_ID="ID";
	public static final String KEY_NOMBRE="NOMBRE";

//ESTE CAMPOS ES OBLIGATORIO EN ANDROID
	//SIMPRE PONLO.
  public static final String KEY_ROWID = "_id";
  private static final String TAG = "TIPOTable";
  private DatabaseHelper mDbHelper;

  static final String DATABASE_TABLE = "tipo";
  static final String DATABASE_TABLE_INDICE = "indice_tipo";

  static final String DATABASE_CREATE = 
			"create table if not exists " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, "
													+ KEY_ID + " integer, "//UNIQUE ON CONFLICT IGNORE, " 
													+ KEY_NOMBRE + " text " + ");"; //Para evitar duplicados
	  
	  
	  
	  

  public TIPOTABLE(Context ctx) {
	  mDbHelper = new DatabaseHelper(ctx);
  }
 
  public void open() throws SQLException {
    mDb = mDbHelper.getWritableDatabase();
  }
 
  public void close() {
    mDbHelper.close();
  }
  
  public boolean delete(String id) {
	    return mDb.delete(DATABASE_TABLE, KEY_ID + "=" + id, null) > 0;
	  }
	  
	  public boolean deleteAll() {
		    return mDb.delete(DATABASE_TABLE, null, null) > 0;
	  }
	  
	  public Cursor fetchAll() throws SQLException {
		   
			String cols = FunctionsUtil.obtenerColumnas(mDb ,DATABASE_TABLE,false);
			String sql="select  " + cols + "   from "+DATABASE_TABLE+ ";";
			  	
			Cursor mCursor = mDb.rawQuery(sql, null);  
			  
			if (mCursor != null) {
				mCursor.moveToFirst();
			}
			return mCursor;
			
		  }
		 
		  public Cursor fetch(int id) throws SQLException {
		   
			  String cols = FunctionsUtil.obtenerColumnas(mDb ,DATABASE_TABLE,false);
			  String sql="select  " + cols + "   from tipo where " + KEY_ID + " = "    + id   +";";
			  	
			  	Cursor mCursor = mDb.rawQuery(sql, null);  
			  
			if (mCursor != null) {
				mCursor.moveToFirst();
			}
			return mCursor;
			
		  }
		  
		  public Cursor fetchWhere(String where) throws SQLException {
			  String cols = FunctionsUtil.obtenerColumnas(mDb ,DATABASE_TABLE,false);
				String sql="select  " + cols + "   from "+DATABASE_TABLE+ ";";

				  if (where!=null && !where.trim().equals("")) sql +=" WHERE " + where; 
				  	
				Cursor mCursor = mDb.rawQuery(sql, null);  
				  
				if (mCursor != null) {
					mCursor.moveToFirst();
				}
				return mCursor;
			 
			  }
		 
		  public long create(TipoBE tipo) {
			    Log.i(TAG, "Inserting record...");
			    ContentValues initialValues = new ContentValues();
			    initialValues.put(KEY_ID,tipo.id);
			    initialValues.put(KEY_NOMBRE,tipo.nombre);
			   
			    return mDb.insert(DATABASE_TABLE, null, initialValues);
			  }
	

}
